import { Injectable } from '@angular/core';
import { HttpService } from '../../service/httpService/http.service';
import { LocalStorageService } from '../../service/localStorageService/local-storage.service';
import { CanActivate, Router } from '@angular/router';
import { refreshTokenUrl } from "../../urlConfig"; // 获取新的访问令牌




@Injectable({
  providedIn: 'root'
})
// 验证token 是否过期
export class CheckoutTokenGuard implements CanActivate {
  newtoken: any = {
    accessToken: '', // 访问令牌
    refreshToken: '',  // 刷新令牌
    role: '', // 权限等级
  }
  constructor(
    public http: HttpService,
    public localStorage: LocalStorageService,
    private router: Router,
  ) { }

  canActivate() {

    const token = this.localStorage.getItem("token");

    if (token === null) {
      this.router.navigate(['/login']);
      console.log('本地token为空，跳到登录页');
      return;
    }
    // this.http.postData(refreshTokenUrl, { refreshToken: token.refreshToken }).subscribe((event: any) => {
    //   console.log(event);
    //   if (event.code === 200) {

    //     this.newtoken.refreshToken = token.refreshToken;
    //     this.newtoken.accessToken = event.result.access_token;
    //     this.newtoken.role = token.role;
    //     this.localStorage.setItem('token', this.newtoken);
    //   }
    // });
    return true;
  }

}
